package com.findviewbydream.evil.eda.Answer;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.findviewbydream.evil.eda.Bean.AnswerBean;
import com.findviewbydream.evil.eda.Data.Data;
import com.findviewbydream.evil.eda.EventBus.AnswerEvent;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.yuyh.easyadapter.abslistview.EasyLVAdapter;
import com.yuyh.easyadapter.abslistview.EasyLVHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Answer
 * 时间：16-8-25 上午9:13
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：答题界面的左侧菜单
 ********************************************/
public class AnswerLeftMenu extends Fragment
{
    private static AnswerLeftMenu answerLeftMenu;

    private View view;

    private ListView listView;
    private Adapter adapter;
    private ArrayList<ItemBean> list;
    private ArrayList<AnswerBean> answerBeanList;

    private Fragment fragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private boolean flag = false;

    public static AnswerLeftMenu newInstance(ArrayList<AnswerBean> list)
    {
        if (answerLeftMenu == null)
            answerLeftMenu = new AnswerLeftMenu();
        answerLeftMenu.answerBeanList = list;
        return answerLeftMenu;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.answer_leftmenu_fragment, container, false);

        initView(view);
        setListView();
        initFragment();

        return view;
    }

    /**
     * 设置ListView
     */
    private void setListView()
    {
        list = new ArrayList<>();

        for (int i = 0; i < answerBeanList.size(); i++)
        {
            list.add(new ItemBean("第" + (i + 1) + "题"));
        }

        adapter = new Adapter(getActivity(), list, R.layout.answer_leftmenu_fragment_item);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                if (flag)
                    listView.getChildAt(0).setBackgroundResource(R.color.transparent);

                fragmentManager = getActivity().getFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();

                switchContent(fragment, AnswerActivity.answerFragmentList.get(i));

                EventBus.getDefault().post(new AnswerEvent("第" + (i + 1) + "题"));
            }
        });

        listView.post(new Runnable()
        {
            @Override
            public void run()
            {
                listView.getChildAt(0).setBackgroundResource(R.color.t_blue);
                flag = true;
            }
        });

    }

    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        fragmentManager = getActivity().getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragment = AnswerActivity.answerFragmentList.get(0);
        fragmentTransaction.add(R.id.container_main, fragment).commit();
    }

    /**
     * 判断fragment是否添加过,如果添加过,就隐藏然后显示,没添加过就添加
     *
     * @param from 旧的fragment
     * @param to   新的fragment
     */
    private void switchContent(Fragment from, Fragment to)
    {
        if (fragment != to)
        {
            fragment = to;
            if (!to.isAdded())
            {    // 先判断是否被add过
                //SLog.print("添加");
                fragmentTransaction.hide(from).add(R.id.container_main, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            }
            else
            {
                //SLog.print("替换");
                fragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        listView = (ListView) view.findViewById(R.id.listView);
    }


    private class Adapter extends EasyLVAdapter<ItemBean>
    {
        public Adapter(Context context, List<ItemBean> list, int... layoutIds)
        {
            super(context, list, layoutIds);
        }

        @Override
        public void convert(EasyLVHolder holder, int position, ItemBean itemBean)
        {
            TextView textView = holder.getView(R.id.textView);
            textView.setText(itemBean.title);
        }
    }


    private class ItemBean
    {
        public ItemBean(String title)
        {
            this.title = title;
        }

        String title;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        fragment = null;
        AnswerActivity.answerFragmentList = null;
        AnswerActivity.anwerBool = null;
    }
}




























